Form
Allow users to provide data or configure options through a group of related input controls where form element wrappers are correctly enclosed in a form tag
#Basic Usage
A form is a group of related input controls that allows users to provide data or configure options. When using the Form component, be sure to wrap your input controls with the Form Element Wrapper component to get consistent styling and access to features like label, help and error messages, invalid and disabled states, etc.
These elements typically include:
- Labels: Clearly identify each input field's purpose with concise, descriptive labels. Position labels left aligned and above the input field by default for better readability and accessibility.
- Data Inputs: Include various structured input types like Input field , Checkbox , Radios , Select, Period Picker, etc. Check the individual component guidelines for specific usage recommendations.
- Help Text: Provide contextual guidance (e.g., tooltips, placeholders, helper text) to assist users in providing the correct information. Place help text near the relevant input field for easy reference.
- Action Bar: Enable users to take action, such as submitting the form or resetting input values. Include primary and secondary action buttons as needed.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onCancel = () => {
setValue("");
setValue1("");
};
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<>
<Form heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Save",
type: "submit",
}}
cancel={{ children: "Cancel", type: "reset", onClick: onCancel }}
noPadding
noBackground
/>
</Form>
</>
);
#Submit Form Example
Enhance the basic form with designated areas for submit. If possible, always include a "Cancel" button for users to exit.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<Form heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Primary",
type: "submit",
}}
noPadding
noBackground
/>
</Form>
);
#Submit Form - Slim Example
A more compact version of the Submit Form, ideal for limited space (e.g Modal) or when the form has fewer fields.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<Form variant="slim" heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Primary",
type: "submit",
}}
noPadding
noBackground
/>
</Form>
);
#Submit Form - Horizontal Example
Optimizes readability by aligning labels horizontally alongside the corresponding input fields. This layout is particularly useful for forms with shorter labels or when space is constrained.
const [value, setValue] = useState("");
const [value1, setValue1] = useState("");
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert("submitting");
};
return (
<Form variant="horizontal" heading="Form heading" onSubmit={onSubmit}>
<FormElementWrapper name="Basic input" label="Basic label">
<InputField
aria-label="My accessible input component"
placeholder="Placeholder"
value={value}
onChange={setValue}
/>
</FormElementWrapper>
<FormElementWrapper name="Basic text area" label="Basic label">
<TextArea
aria-label="My accessible textarea component"
value={value1}
onChange={setValue1}
/>
</FormElementWrapper>
<ActionBar
primary={{
children: "Primary",
type: "submit",
}}
noPadding
noBackground
/>
</Form>
);
| Property | Description | Defined | Value |
|---|---|---|---|
idOptional | stringId applied to the form | ||
childrenOptional | elementElements to be displayed inside the form | ||
headingOptional | stringThe heading for the form | ||
invalidOptional | booleanIs the form in an invalid state | ||
errorsOptional | string[]Error descriptions for describing the invalid state of the form | ||
hideErrorListOptional | booleanShould the error descriptions be hidden | ||
variantOptional | "horizontal" | "regular" | "slim"Form styling options | ||
onSubmitOptional | functionCallback for onSubmit event | ||
data-componentOptional | stringName of the component. Should only be set by components since it needs to stable. Used to track component usage | ||
classNameOptional | stringCustom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | objectStyle object to apply custom inline styles (only intended for special cases) | ||
data-observe-keyOptional | stringUnique string, used by external script e.g. for event tracking |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications